import { Input, input, Node } from "cc";
import { eShiohoEvent, shiohoEventManager } from "../event/shiohoEventManager";
import { baseGuideMediator } from "./baseGuideMediator";
import { guideUtil } from "./guideUtil";
import { GuideWeakClickType, IGuideWeakArgs } from "./iGuide";

interface IWeakGuide {
    guideId: number;
    stepId: number;
    args: IGuideWeakArgs;
    nodes: Node[];
    time: number;
    loopCnt: number;
    isShowing: boolean;
}

export class weakGuideMediator extends baseGuideMediator {

    private _curWeakGuides: IWeakGuide[] = [];

    protected onRun(args?: any) {
        //一个guide允许多个weakstep step检测
        //重复检测
        var guide = this.getGuide(this._guideId, this._stepId);
        if (guide) {
            guide.nodes = [];
            return;
        }

        if (!args.loop)
            args.loop = 1;

        guide = <IWeakGuide>{
            guideId: this._guideId,
            stepId: this._stepId,
            args: args,
            time: 0,
            loopCnt: 0,
            nodes: [],
            isShowing: false,
        }

        this._curWeakGuides.push(guide);
        //打开弱引导页面
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.SwitchWeakPanel, true);

        this.checkNode(guide);
    }

    protected onUpdate(dt: number) {
        for (let i = this._curWeakGuides.length - 1; i >= 0; i--) {
            let guide = this._curWeakGuides[i];

            if (!this.checkNode(guide))
                continue;

            if (guide.isShowing)
                continue;

            //激活才有用吧？
            let activeNode = null;
            for (let j = 0; j < guide.nodes.length; j++) {
                if (guide.nodes[j].activeInHierarchy) {
                    activeNode = guide.nodes[j];
                    break;
                }
            }
            if (!activeNode) {
                if (!!guide.args.alwaysSearchNode)
                    guide.nodes = [];

                continue;
            }

            //额外条件
            if (guide.args.condition && !guide.args.condition())
                continue;

            guide.time += dt;
            if (guide.args.waitTime <= guide.time) {
                guide.isShowing = true;
                //更新手指位置
                shiohoEventManager.getInstance().dispatch(eShiohoEvent.UpdateWeakFingerPos, activeNode.worldPosition);
            }
        }
    }

    protected onComplete() {
        if (this._curWeakGuides.length == 0) {
            //关闭弱引导panel
            shiohoEventManager.getInstance().dispatch(eShiohoEvent.SwitchWeakPanel, false);
        }
    }

    private checkNode(guide: IWeakGuide): boolean {
        if (!guide)
            return false;

        if (guide.nodes.length > 0)
            return true;

        guide.nodes = guideUtil.findNodes(guide.args.locator);

        if (guide.nodes.length > 0) {
            //判断绑定在node还是全局上？
            if (guide.args.clickType == GuideWeakClickType.Global) {
                input.once(Input.EventType.TOUCH_END, () => this.onGuideNodeClick(guide.guideId, guide.stepId), this);
            } else {
                //绑定点击step一次性监听
                guide.nodes.forEach(node => {
                    node.once(Input.EventType.TOUCH_END, () => this.onGuideNodeClick(guide.guideId, guide.stepId), this);
                })
            }
        }
        return guide.nodes.length > 0;
    }

    private onGuideNodeClick(guideId: number, stepId: number) {
        //找到guide
        var guide = this.getGuide(guideId, stepId);
        if (guide) {
            guide.loopCnt++;
            guide.nodes = [];
            guide.time = 0;
            guide.isShowing = false;
            if (guide.args.loop > 0 && guide.loopCnt >= guide.args.loop) {
                //删了
                var idx = this._curWeakGuides.indexOf(guide);
                this._curWeakGuides.splice(idx, 1);
            }
        }

        //下一步
        this.stepOver(guideId, stepId);
    }

    private getGuide(guideId: number, stepId: number): IWeakGuide {
        return this._curWeakGuides.find(v => v.guideId == guideId && v.stepId == stepId);
    }
}


